package com.zhilianbao.leyaogo.utils;

import android.content.res.AssetManager;

import com.bql.convenientlog.CLog;
import com.bql.utils.CheckUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;
import com.zhilianbao.leyaogo.model.entity.AllBean;
import com.zhilianbao.leyaogo.model.entity.BaseBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Json文件工具 <br>
 * Created by Cyarie on 2016/1/13.
 */
public class AddressUtils {

    //JSON文件名
    private final static String FILE = "cityforaddress.json";

    private static ArrayList<BaseBean> list1 = new ArrayList<>();

    private static ArrayList<ArrayList<BaseBean>> list2 = new ArrayList<>();

    private static ArrayList<ArrayList<ArrayList<BaseBean>>> list3 = new ArrayList<>();


    /**
     * 城市列表 用于获取城市编码
     */
    private static ArrayList<BaseBean> cityList = new ArrayList<>();


    /**
     * 先从本地缓存获取城市Json String 如果没有 从assets文件夹下的获取json文件，并解析
     */
    public static void getAddressJsonString() {
        String address;
        if (!CheckUtils.isEmpty(Utils.getDictionary())) {
            address = Utils.getDictionary();
            if (CheckUtils.isEmpty(Utils.getProvinceList()) || CheckUtils.isEmpty(Utils.getCityList()) || CheckUtils.isEmpty(Utils.getDistrictList()))
                parseJsonString(address);
            return;
        } else {
            StringBuilder sb = new StringBuilder();
            try {
                AssetManager assetManager = LeYaoGoApplication.getContext().getAssets();
                BufferedReader bf = new BufferedReader(new InputStreamReader(
                        assetManager.open(FILE)));
                String line;
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            address = sb.toString();
            Utils.putDictionary(address);
            parseJsonString(address);
        }


    }

    /**
     * 解析string 获得 数据流
     *
     * @param json
     */
    public static void parseJsonString(String json) {
        if (json != null) {

            List<AllBean> list = new Gson().fromJson(json, new TypeToken<List<AllBean>>() {
            }.getType());

            for (AllBean allBean : list) {

                ArrayList<BaseBean> list2_ = new ArrayList<>();
                ArrayList<ArrayList<BaseBean>> list3_ = new ArrayList<>();
                for (AllBean.CityEntity cityEntity : allBean.getCityList()) {
                    BaseBean city = new BaseBean();
                    city.setId(cityEntity.getId());
                    city.setName(cityEntity.getCity());
                    list2_.add(city);
                    cityList.add(city);
                    ArrayList<BaseBean> list3__ = new ArrayList<>();

                    if (cityEntity.getCountryList() != null) {
                        for (AllBean.CityEntity.CountryEntity countryEntity : cityEntity.getCountryList()) {
                            BaseBean country = new BaseBean();
                            country.setId(countryEntity.getId());
                            country.setName(countryEntity.getCountry());
                            list3__.add(country);
                        }

                    }
                    list3_.add(list3__);

                }
                list3.add(list3_);
                list2.add(list2_);
                BaseBean bean = new BaseBean();
                bean.setId(allBean.getId());
                bean.setName(allBean.getProvince());
                list1.add(bean);
            }

            Utils.putProvinceList(list1);
            Utils.putCityList(list2);
            Utils.putDistrictList(list3);
            Utils.putCityCodeList(cityList);
        }
        CLog.i("hcy","parse ok... ");
    }

    //    public static ArrayList<BaseBean> getList1() {
    //        return list1;
    //    }
    //
    //    public static ArrayList<ArrayList<BaseBean>> getList2() {
    //        return list2;
    //    }
    //
    //    public static ArrayList<BaseBean> getCityList() {
    //        return cityList;
    //    }
    //
    //
    //    public static ArrayList<ArrayList<ArrayList<BaseBean>>> getList3() {
    //        return list3;
    //    }

    public static int getIndexByIdList1(String id,ArrayList<BaseBean> list) {
        for (int i = 0; i < list.size(); i++) {
            BaseBean baseBean = list.get(i);
            if (baseBean.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static int getIndexByIdList2(String id, int index1,ArrayList<ArrayList<BaseBean>> list2) {
        List<BaseBean> list = list2.get(index1);
        for (int i = 0; i < list.size(); i++) {
            BaseBean baseBean = list.get(i);
            if (baseBean.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static int getIndexByIdList3(String id, int index1, int index2,ArrayList<ArrayList<ArrayList<BaseBean>>> list3) {
        List<BaseBean> list = list3.get(index1).get(index2);
        for (int i = 0; i < list.size(); i++) {
            BaseBean baseBean = list.get(i);
            if (baseBean.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

}
